<?php

namespace pos\services\mall;

use pos\extensions\ApiBaseService;
use common\models\order\OrderModel;
use common\models\order\OrderGoodsModel;
use common\models\order\OrderRefundModel;
/**
 * Desc 首页服务类
 * @author WMX
 */
class MallHomeService extends ApiBaseService
{

    // 个人中心数据统计
    public static function sta()
    {
        try {
            //今日
            $today_start = date('Y-m-d 00:00:00');
            $today_end = date('Y-m-d 23:59:59');
            // $today_start = '2025-02-25 00:00:00';
            // $today_end = '2025-02-25 23:59:59';

            // 销售
            $order = OrderModel::find()
                ->select([
                    'count(*) as order_count',
                    'ifnull(sum(payment_amount),0) as total_payment_amount'
                ])
                ->where(['state' => [2, 3, 4, 7]])
                ->where(['and', ['>=', 'pay_time', $today_start], ['<=','pay_time', $today_end]])
                ->asArray()
                ->one();

            $goods = OrderGoodsModel::find()
                ->alias('g')
                ->leftJoin(OrderModel::tableName() . ' o', 'o.order_sn = g.order_sn')
                ->where(['AND', ['>=', 'o.pay_time', $today_start], ['<=', 'o.pay_time', $today_end], ['in', 'o.state', [2, 3, 4, 7]]])
                ->sum('g.count');
            
            // 退款
            $reund_order = OrderRefundModel::find()
            ->select([
                'count(*) as order_refund_count',
                'ifnull(sum(refund_amount),0) as total_refund_amount'
            ])
            ->where(['state' => [2, 3, 4, 7]])
            ->where(['and', ['>=', 'examine_time', $today_start], ['<=','examine_time', $today_end]])
            ->asArray()
            ->one();

            $refund_goods = OrderGoodsModel::find()
                ->alias('g')
                ->leftJoin(OrderModel::tableName() . ' o', 'o.order_sn = g.order_sn')
                ->where(['AND', ['>=', 'o.pay_time', $today_start], ['<=', 'o.pay_time', $today_end], ['in', 'o.state', [6]]])
                ->sum('g.count');


            // 支付方式
            $pay_order = OrderModel::find()
            ->select([
                'pay_way',
                'count(*) as order_count',
                'sum(payment_amount) as total_payment_amount'
            ])
            ->where(['state' => [2, 3, 4, 7]])
            ->where(['and', ['>=', 'pay_time', $today_start], ['<=','pay_time', $today_end]])
            ->groupBy('pay_way')
            ->asArray()
            ->all();

            $pay = [
                'weixin' => 0,
                'cashe' => 0,
                'balance' => 0
            ];
            foreach($pay_order as $val) {
                if($val['pay_way'] == 1) {
                    $pay['weixin'] = $val['total_payment_amount'];
                }
                if($val['pay_way'] == 3) {
                    $pay['cashe'] = $val['total_payment_amount'];
                }
                if($val['pay_way'] == 4) {
                    $pay['balance'] = $val['total_payment_amount'];
                }
            }


            $data = [
                'order' => array_merge($order, ['goods'=>$goods ?? 0]),
                'refund' => array_merge(['goods'=>$refund_goods ?? 0], $reund_order),
                'pay' => $pay
            ];
            return self::jsonSuccess($data);
        } catch (\Exception $exc) {
            return self::jsonError($exc->getMessage());
        }
    }


    // 个人中心数据统计
    public static function posSta()
    {
        try {
            $headers = \Yii::$app->getRequest()->getHeaders();
            $device = $headers->get('deviceId');
            if (empty($device)) {
                throw new \Exception('收银机号异常');
            }
            //今日
            $today_start = date('Y-m-d 00:00:00');
            $today_end = date('Y-m-d 23:59:59');
            // $today_start = '2025-02-25 00:00:00';
            // $today_end = '2025-02-25 23:59:59';

            // 销售
            $order = OrderModel::find()
                ->select([
                    'count(*) as order_count',
                    'ifnull(sum(payment_amount),0) as total_payment_amount'
                ])
                ->where(['state' => [2, 3, 4, 7]])
                ->where(['device'=> $device])
                ->where(['and', ['>=', 'pay_time', $today_start], ['<=','pay_time', $today_end]])
                ->asArray()
                ->one();

            $goods = OrderGoodsModel::find()
                ->alias('g')
                ->leftJoin(OrderModel::tableName() . ' o', 'o.order_sn = g.order_sn')
                ->where(['o.device'=> $device])
                ->where(['AND', ['>=', 'o.pay_time', $today_start], ['<=', 'o.pay_time', $today_end], ['in', 'o.state', [2, 3, 4, 7]]])
                ->sum('g.count');
            
            // 退款
            $reund_order = OrderRefundModel::find()
            ->alias('g')
            ->leftJoin(OrderModel::tableName() . ' o', 'o.order_sn = g.order_sn')
            ->select([
                'count(*) as order_refund_count',
                'ifnull(sum(refund_amount),0) as total_refund_amount'
            ])
            ->where(['o.device'=> $device])
            ->where(['g.state' => [2, 3, 4, 7]])
            ->where(['and', ['>=', 'g.examine_time', $today_start], ['<=','g.examine_time', $today_end]])
            ->asArray()
            ->one();

            $refund_goods = OrderGoodsModel::find()
                ->alias('g')
                ->leftJoin(OrderModel::tableName() . ' o', 'o.order_sn = g.order_sn')
                ->where(['o.device'=> $device])
                ->where(['AND', ['>=', 'o.pay_time', $today_start], ['<=', 'o.pay_time', $today_end], ['in', 'o.state', [6]]])
                ->sum('g.count');


            // 支付方式
            $pay_order = OrderModel::find()
            ->select([
                'pay_way',
                'count(*) as order_count',
                'sum(payment_amount) as total_payment_amount'
            ])
            ->where(['device'=> $device])
            ->where(['state' => [2, 3, 4, 7]])
            ->where(['and', ['>=', 'pay_time', $today_start], ['<=','pay_time', $today_end]])
            ->groupBy('pay_way')
            ->asArray()
            ->all();

            $pay = [
                'weixin' => 0,
                'cashe' => 0,
                'balance' => 0
            ];
            foreach($pay_order as $val) {
                if($val['pay_way'] == 1) {
                    $pay['weixin'] = $val['total_payment_amount'];
                }
                if($val['pay_way'] == 3) {
                    $pay['cashe'] = $val['total_payment_amount'];
                }
                if($val['pay_way'] == 4) {
                    $pay['balance'] = $val['total_payment_amount'];
                }
            }


            $data = [
                'order' => array_merge($order, ['goods'=>$goods ?? 0]),
                'refund' => array_merge(['goods'=>$refund_goods ?? 0], $reund_order),
                'pay' => $pay
            ];
            return self::jsonSuccess($data);
        } catch (\Exception $exc) {
            return self::jsonError($exc->getMessage());
        }
    }
}
